package com.lldpet.spring.cloud.demo.payment.service.controller;

import com.lldpet.spring.cloud.demo.payment.service.event.PaymentEvent;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @desc：PaymentController
 * @author: guanliang.xue
 * @date: 2021/5/10 10:58
 * @version:
 * @since:
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {

    /**
     * spring cloud bus 的配置
     */
    @Resource
    private BusProperties busProperties;

    /**
     * spring 的事件发布者
     */
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @GetMapping("/publish/event/payment")
    public String publicPaymentEvent(String msg,
                                     @RequestParam(value ="destination", required = false ) String destination){

        // instanceId , spring cloud bus 默认实现的Id
        String instanceId = busProperties.getId();
        PaymentEvent paymentEvent = new PaymentEvent(msg,instanceId,destination);
        applicationEventPublisher.publishEvent(paymentEvent);

        return "success";
    }
}
